/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.area_info.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.thinkgem.jeesite.modules.area_info_his.entity.AreaInfoHis;
import com.thinkgem.jeesite.modules.area_info_his.service.AreaInfoHisService;
import com.thinkgem.jeesite.modules.area_info_rate.entity.TAreaInfoRate;

import com.thinkgem.jeesite.modules.area_info_yesterday.entity.AreaInfoYesterday;
import com.thinkgem.jeesite.modules.area_info_yesterday.service.AreaInfoYesterdayService;
import com.thinkgem.jeesite.modules.lane.entity.Lane;
import com.thinkgem.jeesite.modules.lane.service.LaneService;
import org.apache.poi.ss.formula.ptg.AreaI;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.area_info.entity.AreaInfo;
import com.thinkgem.jeesite.modules.area_info.service.AreaInfoService;

import java.text.NumberFormat;
import java.util.*;

/**
 * 区域信息Controller
 * @author xlp
 * @version 2019-04-11
 */
@Controller
@Lazy(false)
@RequestMapping(value = "${adminPath}/area_info/areaInfo")
public class AreaInfoController extends BaseController {

	@Autowired
	private AreaInfoService areaInfoService;
	@Autowired
	private LaneService laneService;
	@Autowired
	private AreaInfoYesterdayService areaInfoYesterdayService;
	@Autowired
	private AreaInfoHisService areaInfoHisService;
	@ModelAttribute
	public AreaInfo get(@RequestParam(required=false) String id) {
		AreaInfo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = areaInfoService.get(id);
		}
		if (entity == null){
			entity = new AreaInfo();
		}
		return entity;
	}
	
	@RequiresPermissions("area_info:areaInfo:view")
	@RequestMapping(value = {"list", ""})
	public String list(AreaInfo areaInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		/*Page<AreaInfo> page = areaInfoService.findPage(new Page<AreaInfo>(request, response), areaInfo);
		model.addAttribute("page", page);*/
		//查询当前各区域的剩余车位数,目前是A、B两个区域
		List<AreaInfo> areaInfoList = areaInfoService.findList(new AreaInfo());
		List<AreaInfoYesterday> yesterdayList = new ArrayList<AreaInfoYesterday>();
		if (!CollectionUtils.isEmpty(areaInfoList)){
			for (AreaInfo areaInfo1:areaInfoList){
				AreaInfoYesterday yesterday = laneService.getTotalRemainSpace(areaInfo1.getId());
				if (yesterday!=null){
					yesterday.setAreaName(areaInfo1.getAreaName());
					yesterdayList.add(yesterday);
				}
			}
		}
		model.addAttribute("list", yesterdayList);
		List<Lane> list=laneService.surplusCar();
		Map<String,String> map=new HashMap<String, String>();
		for (Lane lane:list){
			if("1".equals(lane.getAreaId())){
				map.put("B",lane.getRemainSpace());
			}else {
				map.put("A",lane.getRemainSpace());
			}
		}
		model.addAttribute("map",map);
		return "modules/area_info/areaInfoList";
	}

	@RequiresPermissions("area_info:areaInfo:view")
	@RequestMapping(value = "form")
	public String form(AreaInfo areaInfo, Model model) {
		model.addAttribute("areaInfo", areaInfo);
		return "modules/area_info/areaInfoForm";
	}

	@RequiresPermissions("area_info:areaInfo:edit")
	@RequestMapping(value = "save")
	public String save(AreaInfo areaInfo, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, areaInfo)){
			return form(areaInfo, model);
		}
		areaInfoService.save(areaInfo);
		addMessage(redirectAttributes, "保存区域信息成功");
		return "redirect:"+Global.getAdminPath()+"/area_info/areaInfo/?repage";
	}
	
	@RequiresPermissions("area_info:areaInfo:edit")
	@RequestMapping(value = "delete")
	public String delete(AreaInfo areaInfo, RedirectAttributes redirectAttributes) {
		areaInfoService.delete(areaInfo);
		addMessage(redirectAttributes, "删除区域信息成功");
		return "redirect:"+Global.getAdminPath()+"/area_info/areaInfo/?repage";
	}
	//车位校验
	@RequiresPermissions("area_info:areaInfo:view")
	@RequestMapping(value = "regexCar")
	public String regexCar(){
		return "modules/area_info/regexCar";
	}

	//实时更新更新车位剩余
	@RequestMapping(value = "surplusCar")
	@ResponseBody
	public Map<String,String> surplusCar(){
		Map<String,String> map=new HashMap<String, String>();
		List<Lane> list=laneService.surplusCar();
		for (Lane lane:list){
			if("1".equals(lane.getAreaId())){
				map.put("B",lane.getRemainSpace());
			}else {
				map.put("A",lane.getRemainSpace());
			}
		}


		return map;
	}
	//获取一天数据信息，转换成折线图
	@RequestMapping(value = "daydate")
	@ResponseBody
	public List<AreaInfoYesterday> ttt(AreaInfoYesterday areaInfoYesterday){
		List<AreaInfoYesterday> list=areaInfoYesterdayService.findList(areaInfoYesterday);
		// 创建一个数值格式化对象
		NumberFormat numberFormat = NumberFormat.getInstance();
		// 设置精确到小数点后2位
		numberFormat.setMaximumFractionDigits(2);
		for (AreaInfoYesterday are:list){
			int j=Integer.parseInt(are.getTotalSpace());
			int i=Integer.parseInt(are.getTotalSpace())-are.getRemainSpace();
			String str=numberFormat.format((float) i / (float) j * 100);
			are.setAreaName(str);
		}
		return  list;
	}

	//保存每小时保存一次数据
	@Scheduled(cron="0 0 0/1 * * ?")
//	@Scheduled(cron="0/30 * * * * ?")
	public void interalTo2(){
		
	}


}